package com.hmall.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.domain.PageDTO;
import com.hmall.domain.dto.ItemDTO;
import com.hmall.domain.po.Item;
import com.hmall.domain.query.ItemPageQuery;
import com.hmall.service.IItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "搜索相关接口")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {

    private final IItemService itemService;

    @ApiOperation("搜索商品")
    @GetMapping("/list")
    public PageDTO<ItemDTO> search(ItemPageQuery query) {

        Page<Item> itemPage = itemService.lambdaQuery()
                // 模糊查询
                .like(StrUtil.isNotBlank(query.getKey()), Item::getName, query.getKey())
                // 品牌查询
                .eq(StrUtil.isNotBlank(query.getBrand()), Item::getBrand, query.getBrand())
                // 分类查询
                .eq(StrUtil.isNotBlank(query.getCategory()), Item::getCategory, query.getCategory())
                // 价格区间查询
                .ge(query.getMaxPrice() != null, Item::getPrice, query.getMaxPrice())
                .le(query.getMinPrice() != null, Item::getPrice, query.getMinPrice())
                // 状态查询
                .eq(Item::getStatus, 1)
                // 按修改
                .orderByDesc(Item::getUpdateTime)
                // 分页
                .page(new Page<>(query.getPageNo(), query.getPageSize()));

        // 返回分页结果
        return PageDTO.of(itemPage, ItemDTO.class);

    }
}
